<?php
header("Content-type:text/html; charset=utf-8;");
require '../../../ppf/fun.php';
require '../../../ppf/pdo_mysql.php';
require '../../../ppf/Filter.php';
date_default_timezone_set("PRC");
$pd=new pdo_mysql();
$filter=new Filter();
session_start();
if(empty($_SESSION['uid'])){
	echo '请登录后再进行操作';exit;
}
$submitMethod=$_SERVER["REQUEST_METHOD"];
if($submitMethod=='POST'){
	$date = date('Y-m-d',time());
	$time = time();
	$type = empty($_POST['type'])?1:$_POST['type'];
    $am_on = $pd->query("select am_on from sys_rest where role=".$_SESSION['role'])->fetchColumn(0);
    $am_on = $date." ".$am_on;//当天上午上班的时间点
	$remark_late_am   = empty($_POST['remark_late_am'])?"":$_POST['remark_late_am'];
	$am_off = $pd->query("select am_off from sys_rest where role=".$_SESSION['role'])->fetchColumn(0);
    $am_off = $date." ".$am_off;//当天上午下班的时间点
	$remark_early_am   = empty($_POST['remark_early_am'])?"":$_POST['remark_early_am'];
	$pm_on = $pd->query("select pm_on from sys_rest where role=".$_SESSION['role'])->fetchColumn(0);
    $pm_on = $date." ".$pm_on;//当天下午上班的时间点
	$remark_late_pm   = empty($_POST['remark_late_pm'])?"":$_POST['remark_late_pm'];
	$pm_off = $pd->query("select pm_off from sys_rest where role=".$_SESSION['role'])->fetchColumn(0);
    $pm_off = $date." ".$pm_off;//当天下午下班的时间点
	$remark_early_pm   = empty($_POST['remark_early_pm'])?"":$_POST['remark_early_pm'];

	$am_on_time = strtotime($am_on);//当天上午上班时间点的时间戳
	$am_off_time = strtotime($am_off);//当天上午上班时间点的时间戳
	$pm_on_time = strtotime($pm_on);//当天下午上班时间点的时间戳
	$pm_off_time = strtotime($pm_off);//当天下午上班时间点的时间戳
	if($time<=$am_on_time){
		$late_am=0;
	}else{
		$late_am=floor(($time-$am_on_time)/60);
	}
	if($time>=$am_off_time){
		$early_am=0;
	}else{
		$early_am=floor(($am_off_time-$time)/60);
	}
	if($time<=$pm_on_time){
		$late_pm=0;
	}else{
		$late_pm=floor(($time-$pm_on_time)/60);
	}
	if($time>=$pm_off_time){
		$early_pm=0;
	}else{
		$early_pm=floor(($pm_off_time-$time)/60);
	}
	$isat = $pd->query("select id from sys_attend where uid='".$_SESSION['uid']."' and date='".$date."'")->fetchColumn(0);
	if($type==1){
		if(!$isat){
			$rs = $pd->exec("insert into sys_attend(uid,date,attend_am_on,late_am,remark_late_am) values(
											'".$_SESSION['uid']."',
											'".$date."',
											'".$time."',
											'".$late_am."',
											'".$remark_late_am."'
			)");
		}else{
			$rs = $pd->exec("update sys_attend set 
											attend_am_on= '".$time."',
											late_am = '".$late_am."',
											remark_late_am = '".$remark_late_am."'
			where id=".$isat);
		}
	}elseif($type==2){
		if(!$isat){
			$rs = $pd->exec("insert into sys_attend(uid,date,attend_am_off,early_am,remark_early_am) values(
											'".$_SESSION['uid']."',
											'".$date."',
											'".$time."',
											'".$early_am."',
											'".$remark_early_am."'
			)");
		}else{
			$rs = $pd->exec("update sys_attend set 
											attend_am_off= '".$time."',
											early_am = '".$early_am."',
											remark_early_am = '".$remark_early_am."'
			where id=".$isat);
		}
	}elseif($type==3){
		if(!$isat){
			$rs = $pd->exec("insert into sys_attend(uid,date,attend_pm_on,late_pm,remark_late_pm) values(
											'".$_SESSION['uid']."',
											'".$date."',
											'".$time."',
											'".$late_pm."',
											'".$remark_late_pm."'
			)");
		}else{
			$rs = $pd->exec("update sys_attend set 
											attend_pm_on= '".$time."',
											late_pm = '".$late_pm."',
											remark_late_pm = '".$remark_late_pm."'
			where id=".$isat);
		}
	}elseif($type==4){
		if(!$isat){
			$rs = $pd->exec("insert into sys_attend(uid,date,attend_pm_off,early_pm,remark_early_pm) values(
											'".$_SESSION['uid']."',
											'".$date."',
											'".$time."',
											'".$early_pm."',
											'".$remark_early_pm."'
			)");
		}else{
			$rs = $pd->exec("update sys_attend set 
											attend_pm_off= '".$time."',
											early_pm = '".$early_pm."',
											remark_early_pm = '".$remark_early_pm."'
			where id=".$isat);
		}
	}


	
	if($rs){
		$tips='ok';
	}else{
		$tips='err';
	}
	echo $tips;exit;

	
}else{
	echo '不允许的表单提交方式，请按正常流程提交表单。';exit;
}
?>